package com.view
{
	import com.adobe.cairngorm.control.CairngormEventDispatcher;
	import com.events.MyEvent;
	import com.model.ModelLocator;
	import com.model.vo.PlayState;
	
	import flash.events.MouseEvent;
	import flash.media.SoundChannel;
	import flash.media.SoundTransform;
	
	import mx.containers.VBox;
	import mx.controls.Button;
	import mx.controls.HSlider;
	
	public class TimelineClass extends VBox
	{
		[Bindable]
		public var percent:Number;
		[Bindable]
		public var timeLabel:String;
		
		public var playPauseBtn:Button;
		
		public var stopBtn:Button;
		public var orginalBtn:Button;
		public var leftChannelBtn:Button;
		public var rightChannelBtn:Button;
		public var timeline:HSlider;
		
		private var sc:SoundChannel;
		
		private var _model:ModelLocator;
		
		
		
		public function TimelineClass()
		{
			super();
			_model = ModelLocator.getInstance();
		}
		
		public function init():void
		{
			initEventListener();
		}
		
		private function initEventListener():void
		{
			
			playPauseBtn.addEventListener(MouseEvent.CLICK, onPlayPauseBtnClick);
//			initBtn.addEventListener(MouseEvent.CLICK, onInitBtnClick);
//			pauseBtn.addEventListener(MouseEvent.CLICK, onPauseBtnClick);
//			playBtn.addEventListener(MouseEvent.CLICK, onPlayBtnClick);
			stopBtn.addEventListener(MouseEvent.CLICK, onStopBtnClick);
			orginalBtn.addEventListener(MouseEvent.CLICK, restoreChannels);
//			leftChannelBtn.addEventListener(MouseEvent.CLICK, toLeftChannel);
//			rightChannelBtn.addEventListener(MouseEvent.CLICK, toRightChannel);
		}
		
		protected function onPlayPauseBtnClick(event:MouseEvent):void
		{
			trace(_model.playState);
			switch(_model.playState)
			{
				
				case PlayState.NONE:
					CairngormEventDispatcher.getInstance().dispatchEvent(new MyEvent(MyEvent.INIT, {lrc:_model.lrc, source:_model.source}));
					playPauseBtn.label = "‖";
					playPauseBtn.toolTip = "暂停";
					break;
				case PlayState.PLAY:
					CairngormEventDispatcher.getInstance().dispatchEvent(new MyEvent(MyEvent.PAUSE));
					playPauseBtn.label = "▶";
					playPauseBtn.toolTip = "播放";
					break;
				case PlayState.PAUSE:
					CairngormEventDispatcher.getInstance().dispatchEvent(new MyEvent(MyEvent.PLAY));
					playPauseBtn.label = "‖";
					playPauseBtn.toolTip = "暂停";
					break;
			}
		}
		
//		protected function onInitBtnClick(event:MouseEvent):void
//		{
//			initBtn.visible = playBtn.visible = false;
//			pauseBtn.visible = true;
//			CairngormEventDispatcher.getInstance().dispatchEvent(new MyEvent(MyEvent.INIT, {lrc:_model.lrc, source:_model.source}));
//		}
//		
//		protected function onPauseBtnClick(event:MouseEvent):void
//		{
//			initBtn.visible = pauseBtn.visible = false;
//			playBtn.visible = true;
//			CairngormEventDispatcher.getInstance().dispatchEvent(new MyEvent(MyEvent.PAUSE));
//		}
//		
//		
//		protected function onPlayBtnClick(event:MouseEvent):void
//		{
//			initBtn.visible = playBtn.visible = false;
//			pauseBtn.visible = true;
//			CairngormEventDispatcher.getInstance().dispatchEvent(new MyEvent(MyEvent.PLAY));
//		}		
		protected function onStopBtnClick(event:MouseEvent):void
		{
			CairngormEventDispatcher.getInstance().dispatchEvent(new MyEvent(MyEvent.STOP));
		}
		
		// 左声道
		public function toLeftChannel(event:MouseEvent):void
		{
			var lc:SoundTransform = new SoundTransform(sc.soundTransform.volume, 0);
			//			lc.leftToLeft=1;
			//			lc.leftToRight=1;
			//			lc.rightToLeft=0;
			//			lc.rightToRight=0;
			lc.pan = -1;
			sc.soundTransform=lc;
			
		}
		
		// 右声道
		public function toRightChannel(event:MouseEvent):void
		{
			var rc:SoundTransform = new SoundTransform(sc.soundTransform.volume, 0);
			//			rc.leftToLeft=0;
			//			rc.leftToRight=0;
			//			rc.rightToLeft=1;
			//			rc.rightToRight=1;
			rc.pan = 1;
			sc.soundTransform=rc;
		}
		
		// 还原
		public function restoreChannels(event:MouseEvent):void
		{
			var mc:SoundTransform=new SoundTransform(sc.soundTransform.volume, 0);
			sc.soundTransform=mc;
		}
		
		public function stop():void
		{
			timeline.value = 0;
			playPauseBtn.label = "▶";
			playPauseBtn.toolTip = "播放";
		}
	}
}